package cn.edu.ctbu.bootstraptablepage.repository;

import cn.edu.ctbu.bootstraptablepage.domain.Student;
import cn.edu.ctbu.bootstraptablepage.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/webapi/login")
public class LoginRestController {

    @Autowired
    StudentService studentService;

    @GetMapping("/user")
    public int user(HttpServletRequest httpServletRequest,String username,String password) {

        //判断用户和密码是否存在
        List<Student> students=studentService.findByNameAndPassword(username,password);

        if(students.size()>0){
            //如果存在，写入session
            HttpSession session=httpServletRequest.getSession();

            session.setAttribute("userid",students.get(0).getId());
            session.setAttribute("user",students.get(0));
            return 1;
        }else {
            //如果不存在，返回出错
            return -1;//不存在用户
        }
    }
}